package com.ktwlsoft.fundmanage.domain.entity;

import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

/**
 * 划款单
 *
 * @author Name on 2019/8/20 11:42
 * @version 1.0
 */
@ApiModel("划款单-TransferApply")
@Entity
@Table(name = "transfer_apply")
@Getter
@Setter
public class TransferApply extends BaseEntity {

    public TransferApply() {

    }

    public TransferApply(Long id, Long tsCode, @Size(max = 30, message = "卖方") String seller,
                         @Size(max = 30, message = "买方") String buyer, @Size(max = 30, message = "监管银行名称") String bankName,
                         @Size(max = 30, message = "监管银行账号(资金入账账号)") String bankCardNum,
                         @Size(max = 30, message = "收款人(权利人或者共有人)") String receiver,
                         @Size(max = 30, message = "收款人关联的人员表主键id") String receiverId,
                         @Size(max = 30, message = "收款人证件号码") String receiverCertificateNumber,
                         @Size(max = 30, message = "收款人收款银行") String receiverBankName,
                         @Size(max = 30, message = "收款人收款银行账号") String receiverBankCardNum, BigDecimal amount, String amountCh,
                         UseType useType, String subBank, BaseFundRecord.Type sourceType, long approvalFlow, long fCode,
                         String receiverCertificateType) {
        this.id = id;
        this.tsCode = tsCode;
        this.seller = seller;
        this.buyer = buyer;
        this.bankName = bankName;
        this.bankCardNum = bankCardNum;
        this.receiver = receiver;
        this.receiverId = receiverId;
        this.receiverBankName = receiverBankName;
        this.receiverBankCardNum = receiverBankCardNum;
        this.amount = amount;
        this.amountCh = amountCh;
        this.state = State.NORMAL;
        this.auditState = AuditStatus.WAIT;
        this.useType = useType;
        this.subBank = subBank;
        this.sourceType = sourceType;
        this.approvalFlow = approvalFlow;
        this.fundInCode = fCode;
        this.receiverCertificateType = receiverCertificateType;
        this.receiverCertificateNumber = receiverCertificateNumber;
    }

    @ApiModelProperty("主键")
    @Id
    @Column(name = "id", columnDefinition = "bigint(30) NOT NULL COMMENT '主键'")
    private Long id;

    @ApiModelProperty("交易编号")
    @Column(name = "ts_code", columnDefinition = "bigint(30) DEFAULT 0 COMMENT '交易编号'")
    @NotNull
    private Long tsCode;

    @ApiModelProperty("工作流id")
    @Column(name = "flowInstanceId", length = 20)
    private String flowInstanceId;

    @ApiModelProperty("卖方")
    @Column(name = "seller", columnDefinition = "varchar(30) DEFAULT NULL COMMENT '卖方'")
    private String seller;

    @ApiModelProperty("卖方Id")
    @Column(name = "sellerId", columnDefinition = "bigint(30) default 0 comment '卖方Id'")
    private long sellerId;

    @ApiModelProperty("买方")
    @Column(name = "buyer", columnDefinition = "varchar(30) DEFAULT NULL COMMENT '买方'")
    private String buyer;

    @ApiModelProperty("买方id")
    @Column(name = "buyer_id", columnDefinition = "bigint(30) default 0 comment '买方Id'")
    private long buyerId;

    @ApiModelProperty("缴款人")
    @Column(name = "payer", columnDefinition = "varchar(30) default null comment '缴款人'")
    private String payer;

    @ApiModelProperty("缴款人Id")
    @Column(name = "payer_id", columnDefinition = "bigint(30) default 0 comment '缴款人Id'")
    private long payerId;

    @ApiModelProperty("监管银行/划款银行")
    @Column(name = "bank_name", columnDefinition = "varchar(30) DEFAULT NULL COMMENT '监管银行名称'")
    private String bankName;

    @ApiModelProperty("监管银行账号(资金入账账号)")
    @Column(name = "bank_card_num", columnDefinition = "varchar(30) DEFAULT NULL COMMENT '监管银行账号(资金入账账号)'")
    private String bankCardNum;

    @ApiModelProperty("收款人(权利人或者共有人)")
    @Column(name = "receiver", columnDefinition = "varchar(30) DEFAULT NULL COMMENT '收款人(权利人或者共有人)'")
    private String receiver;

    @ApiModelProperty("收款人关联的人员表主键id")
    @Column(columnDefinition = "varchar(50) DEFAULT NULL COMMENT '收款人关联的人员表主键id'")
    private String receiverId;

    @ApiModelProperty("收款人证件号码")
    @Column(columnDefinition = "varchar(30) DEFAULT NULL COMMENT '收款人证件号码'")
    private String receiverCertificateNumber;

    @ApiModelProperty("收款方证件类型")
    @Column(length = 20)
    private String receiverCertificateType;

    @ApiModelProperty("收款人收款银行")
    @Column(columnDefinition = "varchar(30) DEFAULT NULL COMMENT '收款人收款银行'")
    private String receiverBankName;

    @ApiModelProperty("银行网点")
    @Column(length = 30)
    private String subBank;

    @ApiModelProperty("收款人收款银行账号")
    @Column(columnDefinition = "varchar(30) DEFAULT NULL COMMENT '收款人收款银行账号'")
    private String receiverBankCardNum;

    @ApiModelProperty("划款金额")
    @Column(columnDefinition = "decimal(16,8) DEFAULT 0 COMMENT '划款金额'")
    private BigDecimal amount = BigDecimal.ZERO;

    @ApiModelProperty("划款金额大写")
    @Column(columnDefinition = "varchar(30) DEFAULT NULL COMMENT '划款金额大写'")
    private String amountCh;

    @ApiModelProperty("作废操作员编号")
    @Column(columnDefinition = "varchar(50) DEFAULT NULL COMMENT '作废操作员编号'")
    private String cancelOperatorId;

    @ApiModelProperty("作废人")
    @Column(length = 20)
    private String cancelOperator;

    @ApiModelProperty("作废时间")
    @Column(name = "cancel_time", columnDefinition = "datetime DEFAULT NULL COMMENT '作废时间'")
    private Date cancelTime;

    @ApiModelProperty("存储状态")
    @Enumerated(EnumType.STRING)
    @Column(name = "state")
    private State state;

    @ApiModelProperty("审核状态")
    @Enumerated(EnumType.STRING)
    @Column(name = "audit_state")
    private AuditStatus auditState;

    @ApiModelProperty("使用类型")
    @Enumerated(EnumType.STRING)
    @Column(name = "use_type")
    private UseType useType;

    @ApiModelProperty("划款类型")
    @Enumerated(EnumType.STRING)
    @Column(name = "transfer_type")
    private TransferType transferType;

    @Column(length = 50)
    @ApiModelProperty("退款划款单Url")
    private String refundUrl;

    @ApiModelProperty("第三方划款单url")
    @Column(length = 50)
    private String thirdApplyUrl;

    @ApiModelProperty("开具人")
    @Column(length = 20)
    private String operator;

    @ApiModelProperty("开具人Id")
    @Column(length = 50)
    private String operatorId;

    @ApiModelProperty("划款来源")
    @Enumerated(EnumType.STRING)
    private BaseFundRecord.Type sourceType;

    @ApiModelProperty("备注")
    @Column(length = 50)
    private String remark;

    @Column(length = 50, columnDefinition = "varchar(50) default null comment '撤销原因'")
    private String cancelRemark;

    @ApiModelProperty("审核意见")
    @Column(length = 50)
    private String authRemark;

    @ApiModelProperty("审核流水号")
    @Column(length = 30)
    private long approvalFlow = 0L;

    @ApiModelProperty("资金缴纳主键")
    private long fundInCode = 0L;

    @ApiModelProperty("资金出账主键")
    private long fundOutCode = 0;

    @ApiModelProperty("第三方划款单类型")
    @Column(length = 50)
    @Enumerated(EnumType.STRING)
    private ThirdTransferType thirdTransferType;

    @ApiModelProperty("是否已划拨")
    private boolean transfered = false;

    @ApiModelProperty("是否过户")
    @Transient
    private boolean transferState = false;

    @ApiModelProperty("是否曾退回")
    private boolean isReturned = false;

    @ApiModelProperty("是否强制撤销")
    private boolean forceCancel = false;

    @ApiModelProperty("是否自动过户")
    @Transient
    private boolean isAutoTransfer;

    @ApiModelProperty("买方不动产权url，多张图片用‘|’分割")
    @Transient
    private String buyerPropertyRightUrl;

    @ApiModelProperty("审核操作人")
    @Column(columnDefinition = "varchar(20) default null comment '审核操作人'")
    private String authOperator;

    @ApiModelProperty("审核操作人Id")
    @Column(columnDefinition = "varchar(30) default null comment '审核操作人Id'")
    private String authOperatorId;

    @ApiModelProperty("审核时间")
    private Date authTime;

    @ApiModelProperty("是否撤销")
    @Transient
    protected boolean isRevoke;

    @ApiModelProperty("划款时间")
    @Temporal(TemporalType.DATE)
    private Date transferDate;

    @ApiModelProperty("银行网点")
    @Column(name = "sub_opening_banck", columnDefinition = "varchar(30) default null comment '银行网点'")
    private String subOpeningBanck;

    @ApiModelProperty("所有权证号, 不要大于30字符")
    @Transient
    private String ownershipNo;

    @ApiModelProperty("买方姓名")
    @Transient
    private String buyerName;

    @ApiModelProperty("买方证件号")
    @Transient
    private String buyerCertificateNumber;

    @ApiModelProperty("卖方姓名")
    @Transient
    private String sellerName;

    @ApiModelProperty("卖方证件号")
    @Transient
    private String sellerCertificateNumber;

    @ApiModelProperty("入账类型")
    @Transient
    private String type;

    @ApiModelProperty("案件退款状态，仅当案件退款时有效")
    @Transient
    private FundSuperviseAccept.RefundState refundState;

    @ApiModelProperty("划款时间")
    @Transient
    private Date payTime;

    public void cancel(String cancelOperator, String cancelOperatorId, Date cancelTime) {
        setState(State.CANCEL);
        setCancelOperator(cancelOperator);
        setCancelOperatorId(cancelOperatorId);
        setCancelTime(cancelTime);
    }

    public void cancelHandled() {
        setState(State.CANCEL_HANDED);
        setForceCancel(true);
    }

    /**
     * 划款单状态类型
     */
    public enum State {

        /**
         * 正常
         */
        NORMAL(1),
        /**
         * 回退
         */
        CANCEL(2),

        /**
         * 作废
         */
        CANCEL_HANDED(3);

        @Getter
        @Setter
        private int power;

        State(int power) {
            this.power = power;
        }
    }

    /**
     * 划款单审核状态类型
     */
    public enum AuditStatus {

        /**
         * 未审核
         */
        WAIT("未审核"),
        /**
         * 审核通过
         */
        PASSED("审核通过"),
        /**
         * 审核未通过
         */
        UNPASSED("审核未通过");

        @Getter
        private String name;

        AuditStatus(String name) {
            this.name = name;
        }
    }

    /**
     * 划款类型
     */
    public enum UseType {
        /**
         * 出账
         */
        FUNDOUT("出账"),
        /**
         * 退款
         */
        REFUND("退款");

        @Getter
        private String name;

        UseType(String name) {
            this.name = name;
        }
    }

    /**
     * 划款类型
     */
    public enum TransferType {
        /**
         * 普通
         */
        NORMAL,
        /**
         * 特殊
         */
        SPECIAL,

        /**
         * 第三方
         */
        THIRD,
        /**
         * 退款划款单
         */
        REFUND;
    }

    /**
     * 第三方划款单的类型
     */
    public enum ThirdTransferType {
        /**
         * 第三方垫付收款
         */
        RECERVE_THIRD_PARTY("第三方垫付收款"), COMPANY_CLEAN_DELEGAGE_THIRD_PARTY("公司清算委托第三方收款"),
        LAW_GARDGE_THIRD_PARTY("司法判决第三方收款"), RELATIVE_THIRD_PARTY("直系亲属代收");

        private String name;

        private ThirdTransferType(String name) {
            this.name = name;
        }
    }
}
